package chances.wechat.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import chances.epg.config.BaseMvcConfig;
import chances.epg.navigator.EpgExceptionHandleResolver;
import chances.epg.navigator.history.NavigateHistoryInteceptor;
import chances.epg.navigator.interceptor.AccessLogInterceptor;
import chances.epg.navigator.interceptor.SubjectFirstPageInterceptor;
import chances.epg.session.UserSessionInterceptor;
import chances.wechat.ctrl.WeChatSessionInterceptor;
import chances.wechat.log.WechatLogger;
import chances.wechat.log.WechatLoggerImpl;

@Configuration
@PropertySource("/WEB-INF/conf/epg.properties")
@PropertySource("/WEB-INF/conf/wechat.properties")
@ComponentScan(basePackages = "chances.epg.navigator.controller," 
		+ "chances.epg.play.ctrl,chances.epg.session.ctrl,"
		+ "chances.epg.session,chances.epg.navigator.service," 
		+ "chances.wechat.ctrl,"
		+ "chances.wechat.applet,"
		+ "chances.wechat.service,chances.wechat.api,"
		+ "chances.wechat.listener,chances.epg.navigator.history,"
		+ "chances.weixin.event")
public class WeChatMvcConfig extends BaseMvcConfig {
	
	@Value("${wechat.gateway.host}")
	private String wgHost = "";
	
	@Value("${wechat.api.backPath:/back}")
	private String backPath = "";
	
	@Value("${wechat.index.url}")
	private String indexUrl;
	
	@Value("${wechat.error.page:/common/wechat/error.jsp}")
	private String errorPage;
	
	@Value("${wechat.gateway.appCode}")
	private String appCode;
	
	@Value("${wechat.gateway.applet.appId}")
	private String appletAppId;
	
	@Value("${wechat.gateway.applet.secret}")
	private String appletSecret;
	
	@Value("${okhttp3.connect.timeout:5}")
	private int connectTimeOut;
	
	@Value("${okhttp3.read.timeout:10}")
	private int readTimeOut;
	
	public UserSessionInterceptor userSessionInterceptor() {
		return new UserSessionInterceptor();
	}

	public AccessLogInterceptor accessLogInterceptor() {
		return new AccessLogInterceptor();
	}

	public NavigateHistoryInteceptor navigateHistoryInteceptor() {
		return new NavigateHistoryInteceptor();
	}

	public SubjectFirstPageInterceptor subjectFirstPageInterceptor() {
		return new SubjectFirstPageInterceptor();
	}

	@Bean
	EpgExceptionHandleResolver exceptionResolver() {
		return new EpgExceptionHandleResolver();
	}

	@Override
	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
		return super.requestMappingHandlerMapping();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new WeChatSessionInterceptor()).addPathPatterns("/**");
		registry.addInterceptor(accessLogInterceptor());
		registry.addInterceptor(subjectFirstPageInterceptor()).addPathPatterns("/**");
		registry.addInterceptor(navigateHistoryInteceptor());
	}


	@Bean
	public WechatLogger weixinLogger() {
		WechatLoggerImpl loggerImpl = new WechatLoggerImpl();
		loggerImpl.setAppId(appCode);
		return loggerImpl;
	}
	

	public String getBackPath() {
		return backPath;
	}

	public void setBackPath(String backPath) {
		this.backPath = backPath;
	}

	public String getWgHost() {
		return wgHost;
	}

	public void setWgHost(String wgHost) {
		this.wgHost = wgHost;
	}

	public String getIndexUrl() {
		return indexUrl;
	}

	public void setIndexUrl(String indexUrl) {
		this.indexUrl = indexUrl;
	}

	public String getErrorPage() {
		return this.errorPage;
	}

	public String getAppCode() {
		return appCode;
	}

	public void setAppCode(String appCode) {
		this.appCode = appCode;
	}

	public int getConnectTimeOut() {
		return connectTimeOut;
	}

	public void setConnectTimeOut(int connectTimeOut) {
		this.connectTimeOut = connectTimeOut;
	}

	public int getReadTimeOut() {
		return readTimeOut;
	}

	public void setReadTimeOut(int readTimeOut) {
		this.readTimeOut = readTimeOut;
	}

	public String getAppletAppId() {
		return appletAppId;
	}

	public String getAppletSecret() {
		return appletSecret;
	}

	public void setAppletAppId(String appletAppId) {
		this.appletAppId = appletAppId;
	}

	public void setAppletSecret(String appletSecret) {
		this.appletSecret = appletSecret;
	}
	
}
